{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 学生上机综合实验（一）- 基础篇\n",
    "\n",
    "**目标：** 本实验旨在综合运用前四讲所学的知识，包括变量的正确使用、字符串的大小写转换、空白字符处理、f字符串格式化以及前缀删除等。请在每个任务下方的代码单元格中完成相应的编程挑战。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 任务一：变量命名与调试\n",
    "\n",
    "本部分旨在考察你对变量命名规则的掌握以及调试基本`NameError`的能力。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.1 修正错误的变量名\n",
    "\n",
    "以下代码中包含了一些不符合Python命名规则的变量名。请找出并修正它们，使代码能够成功运行。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "姓名: Ada\n",
      "第二个数字: 123\n",
      "课程: Programming\n",
      "用户邮箱: test@example.com\n"
     ]
    }
   ],
   "source": [
    "# TODO: 修正以下变量名，使其符合规范\n",
    "first_name = \"Ada\"\n",
    "second_number = 123\n",
    "course_name = \"Programming\"  # 'class'是Python的关键字\n",
    "user_email = \"test@example.com\"\n",
    "\n",
    "# TODO: 修正后，取消下面的注释并运行，以验证你的修改\n",
    "print(f\"姓名: {first_name}\")\n",
    "print(f\"第二个数字: {second_number}\")\n",
    "print(f\"课程: {course_name}\")\n",
    "print(f\"用户邮箱: {user_email}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.2 调试`NameError`\n",
    "\n",
    "下面的代码在运行时会产生`NameError`。请仔细阅读错误信息（Traceback），找到问题所在并修复它。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Python is fun!\n"
     ]
    }
   ],
   "source": [
    "# TODO: 修复代码中的拼写错误\n",
    "message = \"Python is fun!\"\n",
    "print(message)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 任务二：字符串大小写与格式化\n",
    "\n",
    "本部分将练习字符串的大小写处理方法和强大的f字符串。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.1 清理并格式化姓名\n",
    "\n",
    "给定一个包含多余空白且大小写不一致的姓名字符串，请将其清理并格式化为标准的“标题格式”（即每个单词首字母大写）。\n",
    "\n",
    "**预期输出：**\n",
    "```\n",
    "Ada Lovelace\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Ada Lovelace\n"
     ]
    }
   ],
   "source": [
    "messy_name = \"   aDa lOvElAcE  \"\n",
    "\n",
    "# TODO: 在这里编写代码，清理并格式化 messy_name\n",
    "clean_name = messy_name.strip().title()\n",
    "\n",
    "print(clean_name)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.2 使用f-string创建个人简介\n",
    "\n",
    "创建两个变量，`name` 和 `city`，并为它们赋值。然后，使用f-string创建一个完整的句子，介绍这个人的姓名和所在城市。\n",
    "\n",
    "**例如，如果 name=\"张三\", city=\"北京\"，预期输出：**\n",
    "```\n",
    "大家好，我叫张三，我来自北京。\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "大家好，我叫李华，我来自上海。\n"
     ]
    }
   ],
   "source": [
    "# TODO: 在这里填入你的名字和城市\n",
    "name = \"李华\"\n",
    "city = \"上海\"\n",
    "\n",
    "# TODO: 使用 f-string 创建介绍语\n",
    "introduction = f\"大家好，我叫{name}，我来自{city}。\"\n",
    "\n",
    "print(introduction)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 任务三：处理用户输入\n",
    "\n",
    "在实际应用中，用户的输入往往是不规范的。本部分将练习如何清理和验证这些输入。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.1 大小写不敏感的确认\n",
    "\n",
    "假设系统存储的确认命令是`\"yes\"`。用户的输入可能是`\" YES \"`，`\"Yes\"`，`\"yEs\"`等。请编写代码，处理用户输入，使其能够与系统命令正确匹配。\n",
    "\n",
    "**预期输出：**\n",
    "```\n",
    "确认成功！\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "确认成功！\n"
     ]
    }
   ],
   "source": [
    "stored_command = \"yes\"\n",
    "user_input = \"  YeS   \"\n",
    "\n",
    "# TODO: 清理用户输入并修改下面的判断条件\n",
    "if stored_command == user_input.strip().lower(): # 修改这里的判断条件\n",
    "    print(\"确认成功！\")\n",
    "else:\n",
    "    print(\"确认失败。\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 任务四：文本数据清理\n",
    "\n",
    "本部分将综合运用多种字符串方法来处理更复杂的文本数据。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 4.1 删除URL协议前缀\n",
    "\n",
    "给定一个URL，请使用`removeprefix()`方法删除其`https://`前缀。如果你的Python版本低于3.9，可以暂时跳过此题，或者思考其他实现方式。\n",
    "\n",
    "**预期输出：**\n",
    "```\n",
    "www.python.org\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "www.python.org\n"
     ]
    }
   ],
   "source": [
    "full_url = \"https://www.python.org\"\n",
    "\n",
    "# TODO: 使用 removeprefix() 删除前缀\n",
    "simple_url = full_url.removeprefix(\"https://\")\n",
    "\n",
    "print(simple_url)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 4.2 清理多行文本块\n",
    "\n",
    "下面的字符串`raw_data`包含多行文本，其中有些行有不必要的空白，还有些是空行。请编写一个程序来清理它：\n",
    "1.  将字符串分割成多行。\n",
    "2.  去除每一行首尾的空白。\n",
    "3.  过滤掉所有空行。\n",
    "4.  将处理后的有效行用换行符 `\\n` 重新组合成一个干净的字符串。\n",
    "\n",
    "**预期输出：**\n",
    "```\n",
    "[INFO] Application starting...\n",
    "[DEBUG] Connecting to database.\n",
    "[ERROR] Failed to load module 'X'.\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[INFO] Application starting...\n",
      "[DEBUG] Connecting to database.\n",
      "[ERROR] Failed to load module 'X'.\n"
     ]
    }
   ],
   "source": [
    "raw_data = \"\"\"\n",
    "  [INFO] Application starting...   \n",
    "  \n",
    "[DEBUG] Connecting to database.\n",
    "\n",
    "[ERROR] Failed to load module 'X'.\n",
    "\n",
    "\"\"\"\n",
    "\n",
    "# TODO: 在这里编写代码处理 raw_data\n",
    "lines = raw_data.splitlines()\n",
    "clean_lines = []\n",
    "for line in lines:\n",
    "    stripped_line = line.strip()\n",
    "    if stripped_line: # 判断是否为空行\n",
    "        clean_lines.append(stripped_line)\n",
    "\n",
    "clean_data = \"\\n\".join(clean_lines)\n",
    "\n",
    "print(clean_data)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 综合应用挑战\n",
    "\n",
    "恭喜你完成了前面的任务！现在，让我们来挑战一个综合性更强的任务，模拟解析一个简单的日志文件行。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 5.1 解析并格式化日志信息\n",
    "\n",
    "给定一个格式不规范的日志字符串`log_entry`。你需要编写代码来提取关键信息并按要求格式化输出。\n",
    "\n",
    "**处理步骤：**\n",
    "1.  **清理**：去除日志字符串首尾的所有空白字符。\n",
    "2.  **提取用户名**：日志中`user=`后面跟着用户名。请提取出用户名（例如，\"Alice\"）。\n",
    "3.  **格式化输出**：使用f-string生成一条新的、格式化的消息。消息格式为：`\"用户 [用户名] 登录成功！\"`。其中，用户名需要转换为全大写。\n",
    "\n",
    "**预期输出：**\n",
    "```\n",
    "用户 ALICE 登录成功！\n",
    "```\n",
    "**提示：** 你可能需要结合使用`.strip()`、`.split()`和`.removeprefix()`等方法来完成用户名的提取。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "用户 ALICE 登录成功！\n"
     ]
    }
   ],
   "source": [
    "log_entry = \"  INFO: user=Alice; action=login; status=success   \"\n",
    "\n",
    "# TODO: 1. 清理字符串\n",
    "clean_log = log_entry.strip()\n",
    "\n",
    "# TODO: 2. 提取用户名 (这部分最具挑战性，可以分多步完成)\n",
    "# 例如，先用分号分割，再找到包含 \"user=\" 的部分\n",
    "parts = clean_log.split(';')\n",
    "username = \"\"\n",
    "for part in parts:\n",
    "    part = part.strip() # 清理每个部分的前后空格\n",
    "    if part.startswith(\"user=\"):\n",
    "        username = part.removeprefix(\"user=\")\n",
    "        break\n",
    "\n",
    "# TODO: 3. 格式化并打印最终消息\n",
    "formatted_message = f\"用户 {username.upper()} 登录成功！\"\n",
    "\n",
    "print(formatted_message)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 实验结束\n",
    "\n",
    "非常棒！你已经完成了本次综合实验。通过这些练习，你应该对Python中的变量和字符串操作有了更深入的理解。继续努力，编程的世界充满了无限可能！"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}